/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.giraph.examples;

import org.apache.giraph.partition.DefaultPartitionContext;
import org.apache.giraph.graph.Vertex;
import org.apache.giraph.worker.DefaultWorkerContext;
import org.apache.giraph.worker.WorkerContext;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;

import java.io.IOException;

/**
 * Vertex to test the functionality of PartitionContext
 */
public class PartitionContextTestVertex extends
    Vertex<LongWritable, DoubleWritable, FloatWritable,
        DoubleWritable> {
  /** How many compute threads to use in the test */
  public static final int NUM_COMPUTE_THREADS = 10;
  /** How many vertices to create for the test */
  public static final int NUM_VERTICES = 100;
  /** How many partitions to have */
  public static final int NUM_PARTITIONS = 25;

  @Override
  public void compute(Iterable<DoubleWritable> messages) throws IOException {
    TestPartitionContextPartitionContext partitionContext =
        (TestPartitionContextPartitionContext) getPartitionContext();
    partitionContext.counter++;
    if (getSuperstep() > 5) {
      voteToHalt();
    }
  }

  /**
   * PartitionContext for TestPartitionContext
   */
  public static class TestPartitionContextPartitionContext extends
      DefaultPartitionContext {
    /**
     * The counter should hold the number of vertices in this partition,
     * plus the current superstep
     */
    private long counter;

    @Override
    public void preSuperstep(WorkerContext workerContext) {
      counter =
          ((TestPartitionContextWorkerContext) workerContext).superstepCounter;
    }

    @Override
    public void postSuperstep(WorkerContext workerContext) {
      ((TestPartitionContextWorkerContext) workerContext).totalCounter +=
          counter;
    }
  }

  /**
   * WorkerContext for TestPartitionContext
   */
  public static class TestPartitionContextWorkerContext extends
      DefaultWorkerContext {
    /** Current superstep */
    private long superstepCounter;
    /**
     * This counter should hold the sum of PartitionContext's counters
     */
    private long totalCounter;

    @Override
    public void preSuperstep() {
      superstepCounter = getSuperstep();
      totalCounter = 0;
    }

    @Override
    public void postSuperstep() {
      assertEquals(totalCounter,
          NUM_PARTITIONS * superstepCounter + getTotalNumVertices());
    }
  }

  /**
   * Throws exception if values are not equal.
   *
   * @param expected Expected value
   * @param actual   Actual value
   */
  private static void assertEquals(long expected, long actual) {
    if (expected != actual) {
      throw new RuntimeException("expected: " + expected +
          ", actual: " + actual);
    }
  }
}
